//
//  PayAttentionToTheDynamicTableViewCell.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/3.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "PayAttentionToTheDynamicTableViewCell.h"
#import "NewActionContent.h"
#import "NewActionImg.h"
#import "NewActionChannel.h"
#import "NewActionTagModel.h"
#import "FeedListTitleTableViewCell.h"
#import "PostPhotoTableViewCell.h"
#import "TAMacro.h"
#import "DescTableViewCell.h"
#import "FeedListTagsTableViewCell.h"
#import "HomeButtonTableViewCell.h"
#import "CommentTableViewCell.h"
#import "SelfSizeTool.h"
@interface PayAttentionToTheDynamicTableViewCell ()
<
    UITableViewDelegate,
    UITableViewDataSource
>

@property (nonatomic, strong) UITableView *payAttentionToTheDynamicTableView;

@end

@implementation PayAttentionToTheDynamicTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
        [self createView];
        
    }
    return self;
}

- (void)createView {
    
    _payAttentionToTheDynamicTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _payAttentionToTheDynamicTableView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_payAttentionToTheDynamicTableView];
    _payAttentionToTheDynamicTableView.delegate = self;
    _payAttentionToTheDynamicTableView.dataSource = self;
    [_payAttentionToTheDynamicTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    [_payAttentionToTheDynamicTableView registerNib:[UINib nibWithNibName:@"FeedListTitleTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"FeedListTitleTableViewCellReuse"];
    [_payAttentionToTheDynamicTableView registerNib:[UINib nibWithNibName:@"PostPhotoTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"PostPhotoTableViewCellReuse"];
    [_payAttentionToTheDynamicTableView registerNib:[UINib nibWithNibName:@"DescTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"DescTableViewCellReuse"];
    [_payAttentionToTheDynamicTableView registerNib:[UINib nibWithNibName:@"FeedListTagsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"FeedListTagsTableViewCellReuse"];
    [_payAttentionToTheDynamicTableView registerNib:[UINib nibWithNibName:@"HomeButtonTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HomeButtonTableViewCellReuse"];
    [_payAttentionToTheDynamicTableView registerClass:[CommentTableViewCell class] forCellReuseIdentifier:@"CommentTableViewCellReuse"];
    
    
    _payAttentionToTheDynamicTableView.scrollEnabled = NO;
    _payAttentionToTheDynamicTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        return 92;
    } else if (indexPath.row == 1){
        NewActionImg *actionImg = _contentOfNewAction.img[0];
        CGFloat scale = actionImg.w / (SCREEN_WIDTH - 20);
        CGFloat height = actionImg.h / scale;
        return ceilf(height);
    } else if (indexPath.row == 2) {
        if ([_contentOfNewAction.desc isEqualToString:@""]) {
            return 0;
        }
        CGFloat height = [SelfSizeTool selfSizeHeightWithString:_contentOfNewAction.desc width:SCREEN_WIDTH - 40 font:[UIFont systemFontOfSize:14]];
        return height + 25;
    } else if (indexPath.row == 3) {
        if (_contentOfNewAction.tag.count != 0) {
            NSMutableArray *tagNameArray = [NSMutableArray array];
            for (NSInteger i = 0; i < _contentOfNewAction.tag.count; i++) {
                [tagNameArray addObject:_contentOfNewAction.tag[i].content];
            }
            [tagNameArray insertObject:@"#" atIndex:0];
            NSString *tagString = [tagNameArray componentsJoinedByString:@" "];
            CGFloat height = [SelfSizeTool selfSizeHeightWithString:tagString width:SCREEN_WIDTH - 40 font:[UIFont systemFontOfSize:13]];
            return height + 20;
        }
        return 0;
    } else if (indexPath.row == 4){
        return 50;
    } else {
        if (_contentOfNewAction.hotComments.count == 0) {
            return 0;
        } else if (_contentOfNewAction.hotComments.count > 3) {
            return 80;
        }
        return _contentOfNewAction.hotComments.count * 20 + 20;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    _payAttentionToTheDynamicTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (indexPath.row == 0) {
        FeedListTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FeedListTitleTableViewCellReuse"];
        cell.actionContent = _contentOfNewAction;
        return cell;
    } else if(indexPath.row == 1) {
        PostPhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PostPhotoTableViewCellReuse"];
        cell.actionImgArray = _contentOfNewAction.img;
        return cell;
    } else if (indexPath.row == 2) {
        DescTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DescTableViewCellReuse"];
        cell.actionContent = _contentOfNewAction;
        return cell;
    } else if (indexPath.row == 3) {
        FeedListTagsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FeedListTagsTableViewCellReuse"];
        cell.actionContent = _contentOfNewAction;
        return cell;
    } else if (indexPath.row == 4){
        HomeButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeButtonTableViewCellReuse"];
        cell.actionContent = _contentOfNewAction;
        return cell;
    } else {
        CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentTableViewCellReuse"];
        cell.commentArray = _contentOfNewAction.hotComments;
        return cell;
    }
    
}


// 点击方法, 将cell中block的参数传到控制器中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 4) {
//        NSLog(@"分享按钮cell");
    } else {
        NSString *ID = _contentOfNewAction.ID;
        self.testBlock(ID);
//        NSLog(@"跳转页面cell");
    }
}


- (void)prepareForReuse {
    [super prepareForReuse];
    _payAttentionToTheDynamicTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)setContentOfNewAction:(NewActionContent *)contentOfNewAction {
    _contentOfNewAction = contentOfNewAction;
    [_payAttentionToTheDynamicTableView reloadData];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    _payAttentionToTheDynamicTableView.frame = CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height - 10);
//     _payAttentionToTheDynamicTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
